package com.yaorange.certificate.service.impl;

import java.util.Date;
import java.util.List;

import com.yaorange.certificate.domain.NjtUserRegister;
import com.yaorange.certificate.mapper.NjtUserRegisterMapper;
import com.yaorange.common.enums.YesOrNoEnum;
import com.yaorange.common.exception.ServiceException;
import com.yaorange.common.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yaorange.certificate.mapper.NjtUserPerfectMapper;
import com.yaorange.certificate.domain.NjtUserPerfect;
import com.yaorange.certificate.service.INjtUserPerfectService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户完善信息Service业务层处理
 *
 * @author coach tam
 * @date 2024-07-12
 */
@Slf4j
@Service
public class NjtUserPerfectServiceImpl implements INjtUserPerfectService
{
    private final static Logger log = LoggerFactory.getLogger(NjtUserPerfectServiceImpl.class);
    @Autowired
    private NjtUserPerfectMapper njtUserPerfectMapper;
    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;
    /**
     * 查询用户完善信息
     *
     * @param id 用户完善信息主键
     * @return 用户完善信息
     */
    @Override
    public NjtUserPerfect selectNjtUserPerfectById(Long id)
    {
        return njtUserPerfectMapper.selectNjtUserPerfectById(id);
    }

    /**
     * 查询用户完善信息列表
     *
     * @param njtUserPerfect 用户完善信息
     * @return 用户完善信息
     */
    @Override
    public List<NjtUserPerfect> selectNjtUserPerfectList(NjtUserPerfect njtUserPerfect)
    {
        return njtUserPerfectMapper.selectNjtUserPerfectList(njtUserPerfect);
    }

    /**
     * 新增用户完善信息
     *
     * @param njtUserPerfect 用户完善信息
     * @return 结果
     */
    @Override
    public int insertNjtUserPerfect(NjtUserPerfect njtUserPerfect)
    {
        njtUserPerfect.setCreateTime(DateUtils.getNowDate());
        return njtUserPerfectMapper.insertNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 修改用户完善信息
     *
     * @param njtUserPerfect 用户完善信息
     * @return 结果
     */
    @Override
    public int updateNjtUserPerfect(NjtUserPerfect njtUserPerfect)
    {
        njtUserPerfect.setUpdateTime(DateUtils.getNowDate());
        return njtUserPerfectMapper.updateNjtUserPerfect(njtUserPerfect);
    }

    /**
     * 批量删除用户完善信息
     *
     * @param ids 需要删除的用户完善信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserPerfectByIds(Long[] ids)
    {
        return njtUserPerfectMapper.deleteNjtUserPerfectByIds(ids);
    }

    /**
     * 删除用户完善信息信息
     *
     * @param id 用户完善信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserPerfectById(Long id)
    {
        return njtUserPerfectMapper.deleteNjtUserPerfectById(id);
    }


    @Override
    @Transactional
    public int auditNjtUserPerfect(NjtUserPerfect njtUserPerfect)   {
        try {
            //1.更新用户注册表
            NjtUserRegister njtUserRegister = njtUserRegisterMapper.selectNjtUserRegisterById(njtUserPerfect.getId());
            njtUserRegister.setPerfectTime(DateUtils.getNowDate());
            njtUserRegister.setPerfectFlag(YesOrNoEnum.YES.CODE);
            njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);

            //2.更新用户完善表
            njtUserPerfect.setExamineTime(njtUserRegister.getPerfectTime());
            njtUserPerfectMapper.updateNjtUserPerfect(njtUserPerfect);
        } catch (Exception e) {
            log.error("审核用户完善信息异常");
            throw new ServiceException(e.getMessage());
        }
        return 1;
    }
}
